In [4]:
import random
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
def dice():
    number=  [1,2,3,4,5,6]
    return random.choice(number)
 
series = np.array([dice() for x in range(10000)])
print series
from math import factorial


[4 3 5 ..., 2 3 6]

In [2]:
plt.figure(figsize = (20,10))
plt.hist(series,bins = 11,align = 'mid')
plt.xlabel('Dice Number')
plt.ylabel('Occurences')
plt.grid()
plt.show()



In [3]:
print len([x for x in series if x <= 3])/float(len(series))
print np.mean(series)


0.4956
3.5103

In [4]:
print len([x for x in series if x <= 3])/float(len(series))
print np.mean(series)


0.4956
3.5103

In [5]:
def trial():
    number = [1,2,3,4,5,6,7,8,9,10]
    a = random.choice(number)
    if a<= 7:
        return 1
    else:
        return 0

In [6]:
res = [trial() for x in range(10)]
print sum(res)


7

In [7]:
def binomial(number):
    l = []
    for i in range(10000):
        res = [trial() for x in range(10)]
        l.append(sum(res))
    return len([x for x in l if x == number])/float(len(l))
 
print binomial(8)


0.2367

In [8]:
prob = []
for i in range(1,11):
    prob.append(binomial(i))
prob_s = pd.Series(prob,index = range(1,11))
print prob_s


1     0.0002
2     0.0013
3     0.0087
4     0.0373
5     0.1041
6     0.2000
7     0.2674
8     0.2342
9     0.1153
10    0.0283
dtype: float64

In [12]:
print (float(factorial(10))/(factorial(7)*factorial(10-7)))*(0.7**7)*(0.3**3)
print (float(factorial(10))/(factorial(8)*factorial(10-8)))*(0.7**8)*(0.3**2)


0.266827932
0.2334744405

In [13]:
plt.figure(figsize = (20,10))
plt.bar(range(1,11),prob)
plt.grid()
plt.show()



In [16]:
norm = pd.Series(np.random.normal(0,1,100000))
type(norm)
plt.figure(figsize = (20,10))
norm.plot.density()
plt.show()



In [6]:
import quandl
spy_table = quandl.get('LSE/SPY5')
spy = spy_table.loc['2009':'2017',['Last Close']]
spy['log_return'] = np.log(spy['Last Close']).diff()
spy = spy.dropna()

In [7]:
plt.figure(figsize = (20,10))
spy.log_return.plot()
plt.show()



In [8]:
plt.figure(figsize = (20,10))
spy.log_return.plot.density()
plt.show()



In [9]:
de_2 = pd.Series(np.random.normal(0,2,10000),name = 'μ = 0, σ = 2')
de_3 = pd.Series(np.random.normal(0,3,10000),name = 'μ = 0, σ = 3')
de_0 = pd.Series(np.random.normal(0,0.5,10000), name ='μ = 0, σ = 0.5')
mu_1 = pd.Series(np.random.normal(-2,1,10000),name ='μ = -2, σ = 1')
df = pd.concat([de_2,de_3,de_0,mu_1],axis = 1)
plt.figure(figsize=(20,10))
df.plot.density()
plt.show()


<matplotlib.figure.Figure at 0x11601ca90>